﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CashOrCard.Models;
using CashOrCard.Helper;
using MvcPaging;

namespace CashOrCard.Controllers
{
    public class SearchController : Controller
    {
        private const int defaultPageSize = 2;

        private CashOrCardContext dbContext;
        private AdvtSite advtSite;

        public SearchController(CashOrCardContext _db, AdvtSite _advtSite)
        {
            this.dbContext = _db;
            this.advtSite = _advtSite;
        }

        public ActionResult Index(int? page)
        {
            ViewData["category"] = "0";
            ViewData["sitename"] = "";
            ViewData["zipcode"] = "";
            ViewData["rbtcashcard"] = "cash";
            ViewData["cashcard"] = "0";

            int currentPageIndex = page.HasValue ? page.Value : 1;
            ViewData["categoryList"] = Common.GetSiteTypeList();            

            IEnumerable<AdvtSite> sites = dbContext.AdvtSites.Where(x => x.IsEnable.Equals(true));
            return View(sites.OrderByDescending(x=>x.SiteRate).ToPagedList(currentPageIndex,defaultPageSize));
        }

        
        public ActionResult QuickSearch(int? category, string sitename, string zipcode, string rbtcashcard, int? cashcard, int? page)
        {
            ViewData["category"] = category;
            ViewData["sitename"] = sitename;
            ViewData["zipcode"] = zipcode;
            ViewData["rbtcashcard"] = rbtcashcard;
            ViewData["cashcard"] = cashcard;

            int currentPageIndex = page.HasValue ? page.Value : 1;

            ViewData["categoryList"] = Common.GetSiteTypeList();
            IEnumerable<AdvtSite> sites = dbContext.AdvtSites.Where(x => x.IsEnable.Equals(true));
            if (category != null && category != 0)
            {
                sites = sites.Where(x => x.SiteType.SiteTypeId.Equals(category));
            }

            if (!string.IsNullOrEmpty(sitename))
            {
                sites = sites.Where(x => x.SiteName.ToLower().Contains(sitename.ToLower()));
            }

            if (!string.IsNullOrEmpty(zipcode))
            {
                sites = sites.Where(x => x.ZipCode.ToLower().Contains(zipcode.ToLower()));
            }

            if (cashcard != null && cashcard != 0)
            {
                switch (rbtcashcard)
                {
                    case "cash":
                        CashType cash = dbContext.CashTypes.Find(cashcard);
                        sites = sites.Where(x => x.CashTypes.Contains(cash));
                        break;
                    case "card":
                        CardType card = dbContext.CardTypes.Find(cashcard);
                        sites = sites.Where(x => x.CardTypes.Contains(card));
                        break;
                }
            }

            IList<AdvtSite> siteList = sites.OrderByDescending(x=>x.SiteRate).ToPagedList(currentPageIndex, defaultPageSize);

            if (Request.IsAjaxRequest())
                return PartialView("AjaxAdvtSiteList", siteList);
            else
                return View("index", siteList);

            //return View("index", sites.OrderByDescending(x => x.SiteRate).ToPagedList(currentPageIndex, defaultPageSize));
        }
       
        public JsonResult LoadCashCardCombox(string type)
        {   
            switch (type)
            {
                case "cash":
                    List<dynamic> cashList = (from cashType in dbContext.CashTypes
                                              select new
                                              {
                                                  text = cashType.CashTypeName,
                                                  value = cashType.CashTypeId
                                              }).ToList<dynamic>();
                    cashList.Insert(0, new { text = "All", value = "0" });
                    return Json(cashList, JsonRequestBehavior.AllowGet); 
                  
                case "card":
                    List<dynamic> cardList = (from cardType in dbContext.CardTypes
                                select new
                                {
                                    text = cardType.CardTypeName,
                                    value = cardType.CardTypeId
                                }).ToList<dynamic>();
                    cardList.Insert(0, new { text = "All", value = "0" });
                    return Json(cardList, JsonRequestBehavior.AllowGet); 
            }

            return Json(null);
        }

    }
}
